#include "config.h"

Config::Config() {

    BimosSensorPressure *sensor;
    for (int i = 0; i < SENSOR_COUNT; i++){
        sensor = new BimosSensorPressure(i);
        sensorList.insert(i, sensor);
    }
    absolutSensor = new BimosSensorAbsolute(SENSOR_ABSOLUTE_ORDINAL);
    temperaturSensor = new BimosSensorTemperature(SENSOR_TEMPERATUR_ORDINAL);
    setCreateTime(QDateTime::currentDateTime());
    setProjectName("noName");
    setSampleTime(10);}


Config::Config( const Config& other ) {
        *this = other;
}

Config& Config::operator=( const Config& other ) {
    if( this == &other )
        return *this;

    createdTime   = other.createdTime;
    projectName   = other.projectName;

    return *this;
}


/* SETTER */
void Config::setProjectName(QString prName) {
    projectName = createdTime.toString("yyyyMMdd") + "-" + prName;
}

void Config::setSampleTime(int sTime) {
    sampleTime = sTime;
}

void Config::setLogTime(int sTime) {
    logTime = sTime;
}

void Config::setCreateTime(QDateTime createTime){
    this->createdTime = createTime;
}

/* GETTER */
QString Config::getProjectName() {
    return projectName;
}

int Config::getSampleTime() {
    return sampleTime;
}

int Config::getLogTime() {
    return logTime;
}

QDateTime Config::getCreateTime(){
    return createdTime;
}

QDataStream& operator<<(QDataStream &outStream, const Config& data){
    quint8 size = data.sensorList.size();

    outStream << data.projectName;
    outStream << data.createdTime;
    outStream << (quint32) data.sampleTime;
    outStream << size;

    foreach (BimosSensorPressure *sensor, data.sensorList.values()){
        outStream << sensor->getOrdinal();
        outStream << *sensor;
    }
    return outStream;
}

QDataStream& operator>>(QDataStream &inStream, Config& data){
    quint8 size = 0, i = 0;
    BimosSensorPressure *sensor;
    quint32 ordinal;

    inStream >> data.projectName;
    inStream >> data.createdTime;
    inStream >> data.sampleTime;
    // get sensors count from config
    inStream >> size;

    qDeleteAll( data.sensorList );  //  deletes all the values stored in "map"
    data.sensorList.clear();


    for(i = 0; i < size; i++){
        // create new sensor and fill it from config file
        inStream >> ordinal;
        sensor = new BimosSensorPressure(ordinal);
        inStream >> *sensor;
        data.sensorList.insert(ordinal, sensor);
    }

    return inStream;
}




